package com.example.traccarmanager.slice;

import com.example.traccarmanager.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.TextField;
import ohos.agp.window.dialog.CommonDialog;
import ohos.data.DatabaseHelper;
import ohos.data.preferences.Preferences;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class StartFragmentSlice extends AbilitySlice {
    private TextField textField;
    private Button button;
    private String urlStr;
    /**
     *
     */
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_fragment_start);
        textField = (TextField) findComponentById(ResourceTable.Id_field_server);
        urlStr= textField.getText();
        button = (Button) findComponentById(ResourceTable.Id_button_start);
        button.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                button.setEnabled(false);
                textField.getText();
                thread();

            }
        });

    }

    /**
     *
     */
    public void thread() {
        new Thread() {
            @Override
            public void run() {
                super.run();
                url();
            }
        }.start();
    }

    /**
     *
     */
    public void url() {
        try {
            if (urlStr.equals(textField.getText())) {
                URL url = new URL(urlStr);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.connect();
                int responseCode = connection.getResponseCode();
                if (responseCode == HttpURLConnection.HTTP_OK) {
                    InputStream inputStream = connection.getInputStream();
                }
                myuiHandler.sendEvent(InnerEvent.get(1, true));
            } else {
                myuiHandler.sendEvent(InnerEvent.get(1, false));
            }
        } catch (Exception e) {
            myuiHandler.sendEvent(InnerEvent.get(1, false));
        }
    }

    private void onSuccess() {
        DatabaseHelper databaseHelper = new DatabaseHelper(getContext());
        String fileName = "test";
        Preferences preferences = databaseHelper.getPreferences(fileName);
        preferences.putString(com.example.traccarmanager.slice.MainAbilitySlice.PREFERENCE_URL, textField.getText());
        preferences.flushSync();
        Intent intent = new Intent();
        intent.setParam("urls",textField.getText());
        present(new MainFragmentSlice(), intent);
    }

    private void onError() {
        button.setEnabled(true);
        CommonDialog dlg = new CommonDialog(getContext());
        Component layout = LayoutScatter.getInstance(this).parse(ResourceTable.Layout_common_dialog, null, true);
        dlg.setTransparent(true);
        dlg.setContentCustomComponent(layout);
        Button good = (Button) layout.findComponentById(ResourceTable.Id_good);
        good.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                dlg.destroy();
            }
        });
        dlg.show();
    }

    /**
     *
     */
    @Override
    public void onActive() {
        super.onActive();
    }

    /**
     *
     */
    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private MyuiHandler myuiHandler = new MyuiHandler(EventRunner.getMainEventRunner());

    class MyuiHandler extends EventHandler {
        MyuiHandler(EventRunner runner) throws IllegalArgumentException {
            super(runner);
        }

        @Override
        protected void processEvent(InnerEvent event) {
            super.processEvent(event);
            boolean result = (boolean) event.object;
            if (getAbility() != null) {
                if (result) {
                    onSuccess();
                } else {
                    onError();
                }
            }
        }
    }


}
